class Solution:
    def alphabetBoardPath(self, target: str) -> str:
        alpha_map = {}

        row, col = 0, 0
        for i in range(26):
            letter = chr(ord('a')+i)
            if col == 5:
                row += 1
                col = 0
            alpha_map[letter] = (row, col)
            col += 1

        pathStr = ""
        x0, y0 = 0, 0
        for c in target:
            flag = False
            if c == 'z' and (x0, y0) != (5, 0):
                c = 'u'
                flag = True

            x1, y1 = alpha_map[c]
            diffX = abs(x1-x0)
            diffY = abs(y1-y0)
            if x1 > x0:
                pathStr += "".join(["D"]*(diffX))
            if x1 < x0:
                pathStr += "".join(["U"]*(diffX))
            if y1 > y0:
                pathStr += "".join(["R"]*(diffY))
            if y1 < y0:
                pathStr += "".join(["L"]*(diffY))

            if flag:
                pathStr += "D!"
                x0, y0 = 5, 0
            else:
                pathStr += "!"
                x0, y0 = x1, y1

        return pathStr


print(Solution().alphabetBoardPath("code"))
